package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.OrderSetting;
import com.itheima.entity.Result;
import com.itheima.service.OrderSettingService;
import com.itheima.util.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {
    //引用注册中心 OrderSettingService
    @Reference
    private OrderSettingService orderSettingService;

    /**
     * 文件上传
     *
     * @param excelFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {
        try {
            //读取Excel文件
            List<String[]> list = POIUtils.readExcel(excelFile);
            //对list进行判断
            if (list != null && list.size() > 0) {
                //list不为空 将list转为List<OrderSetting> 格式
                List<OrderSetting> orderSettingList = new ArrayList<>();
                for (String[] strings : list) {
                    //将list中的每一个元素进行转化
                    OrderSetting orderSetting = new OrderSetting(new Date(strings[0]), Integer.valueOf(strings[1]));
                    orderSettingList.add(orderSetting);
                }
                //调用添加方法
                orderSettingService.add(orderSettingList);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }

    /**
     * 根据日期查询预约设置数据
     * @param date
     * @return
     */
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date) {
        try {
            //调用getOrderSettingByMonth方法
            List<Map> list = orderSettingService.getOrderSettingByMonth(date);
            //执行成功将list作为参数返回
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            //执行失败
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    /**
     * 根据日期修改可预约人数
     * @param orderSetting
     * @return
     */
    @RequestMapping("/editOrderSettingByDate")
    public Result editOrderSettingByDate(@RequestBody OrderSetting orderSetting) {
        try {
            //调用哪个编辑方法
            orderSettingService.editOrderSettingByDate(orderSetting);
            //执行成功
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //执行失败
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }

}
